/*
 * DeliveryBean.java
 *
 * Created on 27 March 2007, 11:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usyd.comp5348;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import usyd.comp5348.OrderRemoteFactory;
import usyd.comp5348.StockRemoteFactory;

/**
 *
 * @author yzha9368
 */
@Stateless(mappedName="ejb/Delivery")
public class DeliveryBean implements DeliveryRemote, DeliveryLocal {
    
    @EJB(mappedName="ejb/Stock")
    private StockRemote stockBean;
    
    @PersistenceContext
    private EntityManager em;
    
    /** Creates a new instance of DeliveryBean */
    public DeliveryBean() {
        
    }
    
    public int createDelivery(DeliveryInfo delivery) throws DeliveryException {
        try {
            Delivery d = new Delivery(delivery);
            em.persist(d);
            em.flush();
            delivery.setId(d.getId());
            return delivery.getId();
        } catch (Exception e) {
            e.printStackTrace();
            throw new DeliveryException("Could not create new delivery",e);
        }
    }
    
    public void modifyDelivery(DeliveryInfo delivery) throws DeliveryException{
        try {
            Delivery d = new Delivery(delivery);
            em.merge(d);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DeliveryException("Could not modify devliery",e);
        }
    }
    public void deleteDelivery(int id) throws DeliveryException {
        Delivery d;
        try {
            d = em.find(Delivery.class, id);
            if (d!=null)
                em.remove(d);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DeliveryException("Could not delete delivery",e);
        }
        if (d==null)
            throw new DeliveryException("Could not delete delivery(" + id + ") - does not exist");
    }
    
    public void submitDelivery(int id) throws DeliveryException {
        try {
            Delivery delivery = em.find(Delivery.class, id);
            if (delivery==null)
                throw new DeliveryException("Could not find delivery with ID " + id);
            int orderId = delivery.getOrderId();
            OrderRemote orderBean = OrderRemoteFactory.getInstance().getOrderRemote(delivery.getShop());
            OrderInfo order = orderBean.getOrderById(orderId);
            for(OrderLineInfo ol : order.getVideos()) {
                // this is a locally running StockBean
                stockBean.sold(ol.getCatalogue_id(), ol.getQuantity(), order.getWarehouse());
            }
            orderBean.updateOrderStatus(orderId, OrderInfo.DELIVERED);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DeliveryException("Could not process delivery", e);
        }
    }
    
    public DeliveryInfo getDeliveryById(int id) throws DeliveryException {
        try {
            Delivery d = em.find(Delivery.class, id);
            if (d==null)
                return null;
            return d.getDeliveryInfo();
        } catch (Exception e) {
            e.printStackTrace();
            throw new DeliveryException("Could not retrieve Delivery by Id", e);
        }
    }
}
